package cc.mrbird.febs.system.service.impl;
import cc.mrbird.febs.common.entity.DeptTree;
import cc.mrbird.febs.common.entity.DrawlingTree;
import cc.mrbird.febs.common.entity.FebsConstant;
import cc.mrbird.febs.common.entity.QueryRequest;
import cc.mrbird.febs.common.utils.SortUtil;
import cc.mrbird.febs.common.utils.TreeUtil;
import cc.mrbird.febs.system.entity.Dept;
import cc.mrbird.febs.system.entity.Drawing;
import cc.mrbird.febs.system.entity.User;
import cc.mrbird.febs.system.mapper.DrawingMapper;
import cc.mrbird.febs.system.service.IDrawingService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.beans.factory.annotation.Autowired;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *  Service实现
 *
 * @author MrBird
 * @date 2019-07-10 18:56:56
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class DrawingServiceImpl extends ServiceImpl<DrawingMapper, Drawing> implements IDrawingService {

    @Resource
    private DrawingMapper drawingMapper;

    @Override
    public IPage<Drawing> findDrawings(QueryRequest request, Drawing drawing) {
       // LambdaQueryWrapper<Drawing> queryWrapper = new LambdaQueryWrapper<>();
        // TODO 设置查询条件
        Page<Drawing> page = new Page<>(request.getPageNum(), request.getPageSize());
        //return this.page(page, queryWrapper);
        SortUtil.handlePageSort(request, page, "id", FebsConstant.ORDER_ASC, false);
        return this.baseMapper.findDrawlingDetailPage(page, drawing);


    }

    @Override
    public List<Drawing> findDrawings(Drawing drawing) {
	    LambdaQueryWrapper<Drawing> queryWrapper = new LambdaQueryWrapper<>();
		// TODO 设置查询条件
		return this.baseMapper.selectList(queryWrapper);
    }

    @Override
    @Transactional
    public void createDrawing(Drawing drawing) {
        this.save(drawing);
    }

    @Override
    @Transactional
    public void updateDrawing(Drawing drawing) {
        this.saveOrUpdate(drawing);
    }

    @Override
    @Transactional
    public void deleteDrawing(Drawing drawing) {
        LambdaQueryWrapper<Drawing> wapper = new LambdaQueryWrapper<>();
	    // TODO 设置删除条件
	    this.remove(wapper);
	}

    @Override
    public List<DrawlingTree<Drawing>> findDrawingsTrees(Drawing dept) {
        QueryWrapper<Drawing> queryWrapper = new QueryWrapper<>();
       /* if (StringUtils.isNotBlank(dept.getName()))
            queryWrapper.lambda().eq(Drawing::getName, dept.getName());*/
        //queryWrapper.lambda().orderByAsc(Drawing::getId);
        List<Drawing> depts = this.baseMapper.selectList(queryWrapper);
        List<DrawlingTree<Drawing>> trees =  this.convertDepts(depts);
        return TreeUtil.buildDrawlingTree(trees);
    }


    private List<DrawlingTree<Drawing>> convertDepts(List<Drawing> depts){
        List<DrawlingTree<Drawing>> trees = new ArrayList<>();
        depts.forEach(dept -> {
            DrawlingTree<Drawing> tree = new DrawlingTree<>();
            tree.setId(String.valueOf(dept.getId()));
            tree.setParentId(String.valueOf(dept.getParentId()));
            tree.setName(dept.getName());
            tree.setData(dept);
            trees.add(tree);
        });
        return trees;
    }

    @Override
    public int batchDelete(String[] ids) {
        return this.drawingMapper.batchDelete(ids);
    }
}
